﻿#region

using System;
using Singular.Dynamics;
using Singular.Helpers;
using Singular.Managers;
using Singular.Settings;
using Styx;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;
using System.Linq;
using CommonBehaviors.Actions;
/*using Styx.Combat.CombatRoutine;
using Styx.Logic.Combat;*/

#endregion

namespace Singular.ClassSpecific.Druid
{
    public class Feral
    {
        private static DruidSettings Settings
        {
            get { return SingularSettings.Instance.Druid; }
        }

        #region Common

        [Behavior(BehaviorType.Pull, WoWClass.Druid, WoWSpec.DruidFeral, WoWContext.All)]
        public static Composite CreateFeralNormalPull()
        {
            return new PrioritySelector(
                // Ensure Target
                Safers.EnsureTarget(),
                //face target
                Movement.CreateFaceTargetBehavior(),
                // LOS check
                Movement.CreateMoveToLosBehavior(),
                // Auto Attack
                Helpers.Common.CreateAutoAttack(false),
                //Dismount
                new Decorator(ret => StyxWoW.Me.Mounted,
                              Helpers.Common.CreateDismount("Pulling")),
                //Shoot flying targets
                Spell.Cast("Moonfire", 
						   ret => 
						   StyxWoW.Me.CurrentTarget.IsFlying),
                Spell.BuffSelf("Prowl", 
						   ret => 
						   !StyxWoW.Me.HasAura("Might of Ursoc")),
				Spell.Cast("Wild Charge", 
						   ret => 
						   (SpellManager.HasSpell("Wild Charge") && 
						   StyxWoW.Me.CurrentTarget.Distance < 25 && 
						   StyxWoW.Me.CurrentTarget.Distance > 8 && 
						   !StyxWoW.Me.CurrentTarget.IsFlying &&
						   (StyxWoW.Me.HasAura("Cat Form") || 
						   StyxWoW.Me.HasAura("Bear Form")))),
                Spell.Cast("Pounce", 
						   ret => 
						   StyxWoW.Me.HasAura("Prowl")),
				Spell.Cast("Savage Roar", 
						   ret => 
						   StyxWoW.Me.ComboPoints >= 1 && 
						   !StyxWoW.Me.HasAura("Savage Roar")),
				Spell.BuffSelf("Tiger's Fury", 
						   ret => 
						   !StyxWoW.Me.HasAura("Berserk") && 
						   StyxWoW.Me.HasAura("Cat Form") && 
						   StyxWoW.Me.CurrentTarget.HealthPercent >= 40 &&
						   Common.energy <= 30),
				Spell.BuffSelf("Berserk", 
						   ret => 
						   (!StyxWoW.Me.HasAura("Tiger's Fury") && 
						   StyxWoW.Me.CurrentTarget.HealthPercent >= 40 && 
						   Common.energy <= 30 || (StyxWoW.Me.HasAura("Bear Form")))),
                // Move to Melee
                Movement.CreateMoveToMeleeBehavior(true)
                );
        }

        #endregion

        [Behavior(BehaviorType.PreCombatBuffs, WoWClass.Druid, WoWSpec.DruidFeral, WoWContext.All)]
        public static Composite CreateFeralNormalPreCombatBuffs()
        {
            return new PrioritySelector();
        }

        [Behavior(BehaviorType.CombatBuffs, WoWClass.Druid, WoWSpec.DruidFeral, WoWContext.All)]
        public static Composite CreateFeralNormalCombatBuffs()
        {
            return new PrioritySelector();
        }
		
        [Behavior(BehaviorType.Combat, WoWClass.Druid, WoWSpec.DruidFeral, WoWContext.All)]
        public static Composite CreateFeralNormalCombat()
        {				
            return new PrioritySelector(
                //Ensure Target
                Safers.EnsureTarget(),
                //LOS check
                Movement.CreateMoveToLosBehavior(),
                //face target
                Movement.CreateFaceTargetBehavior(),
                //Auto Attack
                Helpers.Common.CreateAutoAttack(false),
				//Ensuring we Stay in Cat Form
                new Decorator(
						   ret => 
						   (StyxWoW.Me.HasAura("Entangling Roots") || 
						   StyxWoW.Me.HasAura("Mass Entanglement") || 
						   StyxWoW.Me.HasAura("Earthbind") || 
						   StyxWoW.Me.HasAura("Frost Nova") || 
						   StyxWoW.Me.HasAura("Ice Ward") || 
						   StyxWoW.Me.HasAura("Frost Shock") ||
						   StyxWoW.Me.HasAura("Ice Trap") ||
						   StyxWoW.Me.HasAura("Entrapment") ||
						   StyxWoW.Me.HasAura("Web") ||
						   StyxWoW.Me.HasAura("Chains of Ice") ||
						   StyxWoW.Me.HasAura("Void Tendril's Grasp") ||
						   StyxWoW.Me.HasAura("Freeze")),
                    new PrioritySelector(
                        Spell.Cast("Cat Form",
                            ret => 
							!StyxWoW.Me.HasAura("Might of Ursoc")))),
				Spell.Cast("Cat Form", 
						   ret => 
						   !StyxWoW.Me.HasAura("Cat Form") &&
						   !StyxWoW.Me.HasAura("Might of Ursoc") &&
						   !StyxWoW.Me.IsCasting),
				//Defensive Stuff
				Spell.Cast("Mighty Bash", 
						   ret => 
						   SpellManager.HasSpell("Mighty Bash") &&
						   StyxWoW.Me.CurrentTarget.HealthPercent >= 20 &&
						   !StyxWoW.Me.CurrentTarget.HasAura("Pounce")),
				Spell.BuffSelf("Barkskin", 
						   ret => 
						   SpellManager.HasSpell("Barkskin") && 
						   StyxWoW.Me.HealthPercent <= 80 &&
						   (StyxWoW.Me.CurrentTarget.HealthPercent > 20 ||
						   (Unit.UnfriendlyUnitsNearTarget(8f).Count() >= 2))),
				Spell.Heal("Healing Touch", 
						   ret => 
						   (StyxWoW.Me.HasAura("Predatory Swiftness") && 
						   StyxWoW.Me.HealthPercent <= 80 || 
						   (StyxWoW.Me.HasAura("Nature's Swiftness")))),
				/*Spell.Heal("Rejuvenation", 
						   ret => 
						   (StyxWoW.Me.HealthPercent <= 70 && 
						   StyxWoW.Me.HealthPercent >= 50 && 
						   !StyxWoW.Me.HasAura("Rejuvenation") && 
						   !StyxWoW.Me.HasAura("Might of Ursoc") ||
						   (SpellManager.Spells["Might of Ursoc"].Cooldown &&
						   SpellManager.Spells["Nature's Swiftness"].Cooldown &&
						   !StyxWoW.Me.HasAura("Predatory Swiftness") &&
						   StyxWoW.Me.HealthPercent < 30))),*/
				Spell.BuffSelf("Nature's Swiftness", 
						   ret => 
						   SpellManager.HasSpell("Nature's Swiftness") && 
						   StyxWoW.Me.HealthPercent < 50 &&
						   !StyxWoW.Me.HasAura("Predatory Swiftness")),
				Spell.BuffSelf("Renewal", 
						   ret => 
						   SpellManager.HasSpell("Renewal") && 
						   StyxWoW.Me.HealthPercent < 50),
				Spell.BuffSelf("Survival Instincts", 
						   ret => 
						   StyxWoW.Me.HealthPercent <= 40),
				Spell.BuffSelf("Might of Ursoc", 
						   ret => 
						   StyxWoW.Me.HealthPercent <= 30 &&
						   !StyxWoW.Me.HasAura("Predatory Swiftness")),
				Spell.BuffSelf("Frenzied Regeneration", 
						   ret => 
						   StyxWoW.Me.HasAura("Bear Form") && 
						   Common.energy >= 60 && 
						   StyxWoW.Me.HealthPercent < 80),
				//Getting out of Snares
				Spell.Cast("Stampeding Roar", 
						   ret => 
						   StyxWoW.Me.CurrentTarget.Distance > 25 &&
						   SpellManager.Spells["Dash"].Cooldown ||
						   (StyxWoW.Me.CurrentTarget.Distance <= 25 &&
						   SpellManager.Spells["Wild Charge"].Cooldown)),	
				// StyxWoW.Me.CurrentTarget.Fleeing
				Spell.Cast("Dash", 
						   ret => 
						   StyxWoW.Me.CurrentTarget.Distance > 25 &&
						   SpellManager.Spells["Stampeding Roar"].Cooldown ||
						   (StyxWoW.Me.CurrentTarget.Distance <= 25 &&
						   SpellManager.Spells["Wild Charge"].Cooldown)),	
				//Energy Management
				Spell.BuffSelf("Tiger's Fury", 
						   ret => 
						   !StyxWoW.Me.HasAura("Berserk") && 
						   StyxWoW.Me.HasAura("Cat Form") && 
						   StyxWoW.Me.CurrentTarget.HealthPercent >= 40 &&
						   Common.energy <= 30),
				Spell.BuffSelf("Berserk", 
						   ret => 
						   (!StyxWoW.Me.HasAura("Tiger's Fury") && 
						   StyxWoW.Me.CurrentTarget.HealthPercent >= 40 && 
						   Common.energy <= 30 || (StyxWoW.Me.HasAura("Bear Form")))),
				//AOE Stuff
				Spell.Cast("Thrash", 
						   ret => 
						   Unit.UnfriendlyUnitsNearTarget(8f).Count() >= 3 && 
						   (!StyxWoW.Me.CurrentTarget.HasMyAura("Thrash") &&
						   StyxWoW.Me.HasAura("Cat Form") &&
						   Common.energy > 80 ||
						   (Unit.UnfriendlyUnitsNearTarget(8f).Count() >= 3 && 
						   StyxWoW.Me.HasAura("Bear Form")))),
				Spell.Cast("Swipe", 
						   ret => 
						   (Unit.UnfriendlyUnitsNearTarget(8f).Count() >= 3 && 
						   StyxWoW.Me.CurrentTarget.HasMyAura("Thrash") && 
						   StyxWoW.Me.HasAura("Cat Form") &&
						   Common.energy > 60 || 
						   (Unit.UnfriendlyUnitsNearTarget(8f).Count() >= 3 &&
						   StyxWoW.Me.HasAura("Bear") &&
						   Common.energy >= 95))),
				//Bridging Distance
				Spell.Cast("Wild Charge", 
						   ret => 
						   (SpellManager.HasSpell("Wild Charge") && 
						   StyxWoW.Me.CurrentTarget.Distance < 25 && 
						   StyxWoW.Me.CurrentTarget.Distance > 8 && 
						   (StyxWoW.Me.HasAura("Cat Form") || 
						   StyxWoW.Me.HasAura("Bear Form")))),
				//Interupt
				Spell.Cast("Skull Bash", 
						   ret => 
						   (SpellManager.HasSpell("Skull Bash") &&
						   StyxWoW.Me.CurrentTarget.IsCasting) &&
						   (StyxWoW.Me.HasAura("Cat Form") || 
						   StyxWoW.Me.HasAura("Bear Form"))),
				//Other
                Spell.Cast("Faerie Fire",
                           ret =>
                           !StyxWoW.Me.CurrentTarget.HasMyAura("Weakened Armor") ||
                           (StyxWoW.Me.CurrentTarget.HasMyAura("Weakened Armor") &&
                           StyxWoW.Me.CurrentTarget.GetAuraByName("Weakened Armor").StackCount < 3)),
				//Combo Point Finshers/Consumers
				Spell.Cast("Ravage",
						   ret =>
						   SpellManager.HasSpell("Ravage") && 
						   (StyxWoW.Me.HasAura("Stampede"))),
                Spell.Cast("Savage Roar", 
						   ret => 
						   SpellManager.HasSpell("Savage Roar") && 
						   StyxWoW.Me.ComboPoints >= 1 && 
						   !StyxWoW.Me.HasAura("Savage Roar") && 
						   StyxWoW.Me.HasAura("Cat Form")),
                Spell.Cast("Ferocious Bite", 
						   ret => 
						   (StyxWoW.Me.CurrentTarget.GetAuraTimeLeft
						   ("Rip", true).TotalSeconds >= 8 && 
						   StyxWoW.Me.ComboPoints >= 5 && Common.energy >= 25 ||
						   (StyxWoW.Me.CurrentTarget.HealthPercent <= 25))),
				Spell.Cast("Rip", 
						   ret => 
						   SpellManager.HasSpell("Rip") && 
						   StyxWoW.Me.ComboPoints >= 5 && 
						   StyxWoW.Me.CurrentTarget.GetAuraTimeLeft
						   ("Rip", true).TotalSeconds <= 5 &&
						   StyxWoW.Me.CurrentTarget.HealthPercent >= 30),
				//Combo Point Generators
                Spell.Cast("Shred", 
						   ret => 
						   StyxWoW.Me.CurrentTarget.MeIsSafelyBehind && 
						   StyxWoW.Me.CurrentTarget.HasAura("Rake") && 
						   StyxWoW.Me.ComboPoints < 5 &&
						   Common.energy >= 40),
                Spell.Cast("Mangle", 
						   ret => 
						   (StyxWoW.Me.HasAura("Bear Form") || 
						   (!StyxWoW.Me.CurrentTarget.MeIsSafelyBehind && 
						   StyxWoW.Me.CurrentTarget.HasAura("Rake") && 
						   StyxWoW.Me.ComboPoints < 5))),
				Spell.Cast("Rake", ret =>
						   StyxWoW.Me.CurrentTarget.GetAuraTimeLeft
						   ("Rake", true).TotalSeconds < 4),
				//Bear Form Stuff
				Spell.Cast("Lacerate", 
						   ret => 
						   StyxWoW.Me.HasAura("Bear Form") && 
						   !StyxWoW.Me.HasMyAura("Lacerate", 3) &&
						   !StyxWoW.Me.HasAura("Berserk")),
				Spell.Cast("Maul", 
						   ret => 
						   StyxWoW.Me.HasAura("Bear Form") && 
						   StyxWoW.Me.HealthPercent >= 80 && 
						   Common.energy >= 95),
				Spell.Cast("Thrash", 
						   ret => 
						   StyxWoW.Me.HasAura("Bear Form") &&
						   !StyxWoW.Me.HasAura("Berserk")),
				
            Movement.CreateMoveToMeleeBehavior(true)
            )
            ;
        }
    }
}